﻿/*
 * 对外提供json rpc的网络通讯服务
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jayrock.JsonRpc.Web;
using WH.NurseSys.BLL;
using WH.NurseSys.Model;
using Jayrock.JsonRpc;
using WH.NurseSys.Web.Model;
using System.Web.Script.Serialization;
using System.Diagnostics;

namespace WH.NurseSys.Web.Services
{
    public class Service : JsonRpcHandler
    {
        //测试网络连通性
        [JsonRpcMethod]
        public bool echo()
        {
            return true;
        }

        //登录
        [JsonRpcMethod]
        public string login(string username, string pwd)
        {
            int code;
            JavaScriptSerializer jss=new JavaScriptSerializer();
            LoginResponse response = new LoginResponse();
            try
            {
                Doctor doctor = new DoctorBLL().Login(username, pwd, out code);
                response.code = code;
                switch (code)
                {
                    case 1:
                        Dept dept = new DeptBLL().GetById(doctor.DeptId);
                        response.id = doctor.Id;
                        response.username = doctor.Username;
                        response.nickname = doctor.Nickname;
                        response.rank = doctor.Rank;
                        response.dept_id = doctor.DeptId;
                        response.dept_name = dept.Name;
                        response.error = null;
                        break;                  
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                response.code = 4;
                response.error = e.Message;
            }
            return jss.Serialize(response);           
        }

        //获取科室列表
        [JsonRpcMethod]
        public string get_dept_list()
        {
            List<Dept> deptList = new DeptBLL().GetList();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(deptList);          
        }

        //登记新的病人,如已有，就update，无则insert
        //admission_date:入院日期
        [JsonRpcMethod]
        public bool save_patient(string id,string dept_id,string name,bool gender,int age,string age_unit,string doctor,string bed_no,string inpatient_no,string clinic_diag,string admission_date,bool in_hosp)
        {
            try
            {
                Guid patientId = Guid.Parse(id);
                Guid deptId = Guid.Parse(dept_id);
                PatientBLL patientBLL=new PatientBLL();
                Patient patient = patientBLL.GetById(patientId);
                if (patient == null)
                {
                    patient=new Patient();
                    patient.Age=age;
                    patient.AgeUnit=age_unit;
                    patient.BedNo=bed_no;
                    patient.ClinicDiag=clinic_diag;
                    patient.DeptId=deptId;
                    patient.Doctor=doctor;
                    patient.Gender=gender;
                    patient.Id=patientId;
                    patient.InDate=DateTime.Now;
                    patient.OutDate = DateTime.Now;
                    patient.InHosp=in_hosp;
                    patient.InpatientNo=inpatient_no;
                    patient.Name=name;
                    if (patientBLL.Insert(patient))
                        return true;
                    else
                        return false;
                }
                else
                {
                    patient.Age = age;
                    patient.AgeUnit = age_unit;
                    patient.BedNo = bed_no;
                    patient.ClinicDiag = clinic_diag;
                    patient.DeptId = deptId;
                    patient.Doctor = doctor;
                    patient.Gender = gender;
                    patient.Id = patientId;
                    patient.InDate = DateTime.Now;
                    patient.OutDate = DateTime.Now;
                    patient.InHosp = in_hosp;
                    patient.InpatientNo = inpatient_no;
                    patient.Name = name;
                    if (patientBLL.Update(patient))
                        return true;
                    else
                        return false;
                }                
            }
            catch (Exception e)
            {
                string log = string.Format("{0} : service.ashx save_patient()错误,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e.Message);
                Trace.WriteLine(log);
                return false; 
            }
        }

        //登记病人出院
        [JsonRpcMethod]
        public bool leave_hosp(string patient_id)
        {
            try
            {
                Guid id = Guid.Parse(patient_id);
                PatientBLL patientBLL=new PatientBLL();
                Patient patient = patientBLL.GetById(id);
                if (patient == null)
                    return false;
                patient.InHosp = false;
                if (patientBLL.Update(patient))
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                string log = string.Format("{0} : service.ashx leave_hosp()错误,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e.Message);
                Trace.WriteLine(log);
                return false; 
            }
        }

        //按科室获取在院的病人列表
        [JsonRpcMethod]
        public string get_patient_list(string dept_id)
        {
            Guid deptId = Guid.Parse(dept_id);
            List<Patient> patientList = new PatientBLL().GetInListByDept(deptId);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(patientList);           
        }

        //上传体温检查数据
        [JsonRpcMethod]
        public string upload_temperature(string id, string patient_id, float value, string exec_time)
        {
            string response=string.Empty;
            try
            {
                Temperature temperature = new Temperature();
                temperature.Id = Guid.Parse(id);
                temperature.PatientId = Guid.Parse(patient_id);
                temperature.Value = value;
                temperature.ExecTime = DateTime.Parse(exec_time);
                TemperatureBLL temperatureBLL = new TemperatureBLL();                
                if (temperatureBLL.Exist(temperature.Id))
                {
                    if (temperatureBLL.Update(temperature))
                        response = "{\"code\":2,\"msg\":\"\"}";
                    else
                        response = "{\"code\":3,\"msg\":\"\"}";     
                }
                else
                {
                    if (temperatureBLL.Insert(temperature))                    
                        response = "{\"code\":1,\"msg\":\"\"}";                     
                    else                    
                        response = "{\"code\":3,\"msg\":\"\"}";
                }
            }
            catch (Exception e)
            {
                string log = string.Format("{0} : service.ashx upload_temperature()错误,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e.Message);
                Trace.WriteLine(log);
                response = string.Format("{{\"code\":4,\"msg\":{0}}}", e.Message);               
            }
            return response;           
        }
    
        //上传血糖检查数据
        [JsonRpcMethod]
        public string upload_glucose(string id, string patient_id, float value, int measure_context, string exec_time)
        {
            string response = string.Empty;
            try
            {
                Glucose glucose = new Glucose();
                glucose.Id = Guid.Parse(id);
                glucose.PatientId = Guid.Parse(patient_id);
                glucose.Value = value;
                glucose.MeasureContext = measure_context;
                glucose.ExecTime = DateTime.Parse(exec_time);
                GlucoseBLL glucoseBLL = new GlucoseBLL();
                if (glucoseBLL.Exist(glucose.Id))
                {
                    if (glucoseBLL.Update(glucose))
                    {
                        Trace.WriteLine(string.Format("{0} : {1} update glucose成功,patient_id={2},id={3}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),GetType(),patient_id,id));
                        response = "{\"code\":2,\"msg\":\"\"}";
                    }
                    else
                        response = "{\"code\":3,\"msg\":\"\"}";
                }
                else
                {
                    if (glucoseBLL.Insert(glucose))
                    {
                        Trace.WriteLine(string.Format("{0} : {1} insert glucose成功,patient_id={2},id={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), GetType(), patient_id, id));
                        response = "{\"code\":1,\"msg\":\"\"}";
                    }
                    else
                        response = "{\"code\":3,\"msg\":\"\"}";
                }
            }
            catch (Exception e)
            {
                string log = string.Format("{0} : service.ashx upload_glucose()错误,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e.Message);
                Trace.WriteLine(log);
                response = string.Format("{{\"code\":4,\"msg\":{0}}}", e.Message);
            }
            return response;   
        }
    
        //上传血压检查数据
        [JsonRpcMethod]
        public string upload_blood_pressure(string id, string patient_id, int sys, int dia, int pulse, string exec_time)
        {
            string response=string.Empty;
            try
            {
                BloodPressure bloodPressure = new BloodPressure();
                bloodPressure.Id = Guid.Parse(id);
                bloodPressure.PatientId = Guid.Parse(patient_id);
                bloodPressure.Sys = sys;
                bloodPressure.Dia = dia;
                bloodPressure.Pulse = pulse;
                bloodPressure.ExecTime = DateTime.Parse(exec_time);
                BloodPressureBLL bloodPressureBLL = new BloodPressureBLL();
                if (bloodPressureBLL.Exist(bloodPressure.Id))
                {
                    if(bloodPressureBLL.Update(bloodPressure))
                        response = "{\"code\":2,\"msg\":\"\"}";
                    else
                        response = "{\"code\":3,\"msg\":\"\"}";
                }
                else
                {
                    if (bloodPressureBLL.Insert(bloodPressure))
                        response = "{\"code\":1,\"msg\":\"\"}";
                    else
                        response = "{\"code\":3,\"msg\":\"\"}";
                }
            }
            catch (Exception e)
            {
                string log = string.Format("{0} : service.ashx upload_blood_pressure()错误,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e.Message);
                Trace.WriteLine(log);
                response = string.Format("{{\"code\":4,\"msg\":{0}}}", e.Message);
            }
            return response;
        }
    }
}